﻿using FlowCard.PrintCard.Models;
using FlowCard.PrintCard.Services;
using Microsoft.AspNetCore.Mvc;
using System.Text;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly IGenerateFile generate;
        public ValuesController(IGenerateFile generate) 
        {
            this.generate = generate;
        }

        /// <summary>
        /// 获取零件示例数据
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        [HttpPost(Name = "GetDemoData")]
        public IActionResult GetDemoData()
        {
            // 示例数据源
            var print = new Print()
            {
                PrintName = "张三",
                FormatCode = "V_123.01.2.11",
                ProcessCardName = "零件工艺流程卡（正面）",
                PrintHeads = new List<PrintHead>()
                {
                    new PrintHead
                    {
                        ProjectCode = "PROJ202410310005",
                        ProductCode = "2024-010-030-0001",
                        PartDrawingNo = "0403",
                        PartName = "零件1",
                        ProdBatch = "001",
                        ProdQuantity = "10",
                        StartDate = "2024-09-27",
                        MaterialCode = "P-124546222",
                        MaterialName = "新版Iphone16 Plus",
                        MaterialBatchNo = "36545-98652",
                        MaterialSpec = "5988打啥工 655地方",
                        PartsManufacturable = "10",
                        MaterialItemNo = "ASF78945",
                        ProcessCardNo = "202411010001",//流程卡号：
                        Items = new List<PrintHeadItem>()
                        {
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0005",ProcessName="测试1",WorkcenterName="工作中心1"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0010",ProcessName="测试2",WorkcenterName="工作中心2"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0015",ProcessName="测试3",WorkcenterName="工作中心3"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0020",ProcessName="测试4",WorkcenterName="工作中心4"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0025",ProcessName="测试5",WorkcenterName="工作中心5"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0030",ProcessName="测试6",WorkcenterName="工作中心6"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0035",ProcessName="测试7",WorkcenterName="工作中心7"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0040",ProcessName="测试8",WorkcenterName="工作中心8"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0045",ProcessName="测试9",WorkcenterName="工作中心9"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0050",ProcessName="测试10",WorkcenterName="工作中心10"},
                            new PrintHeadItem{ ProcessCardNo="202411010001",ProcessNumber="0055",ProcessName="测试11",WorkcenterName="工作中心11"},
                        }
                    },
                    new PrintHead
                    {
                        ProjectCode = "PROJ202410310007",
                        ProductCode = "2024-011-001-0004",
                        PartDrawingNo = "0403",
                        PartName = "零件2",
                        ProdBatch = "001",
                        ProdQuantity = "10",
                        StartDate = "2024-09-27",
                        MaterialCode = "P-20241534645",
                        MaterialName = "最新版华为Mate60 Plus",
                        MaterialBatchNo = "36545-98652",
                        MaterialSpec = "5988打啥工 655地方",
                        PartsManufacturable = "10",
                        MaterialItemNo = "ASF78945",
                        ProcessCardNo = "202411010002",
                        Items = new List<PrintHeadItem>()
                        {
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0005",ProcessName="测试1",WorkcenterName="工作中心a"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0010",ProcessName="测试2",WorkcenterName="工作中心b"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0015",ProcessName="测试3",WorkcenterName="工作中心c"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0020",ProcessName="测试4",WorkcenterName="工作中心d"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0025",ProcessName="测试5",WorkcenterName="工作中心e"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0030",ProcessName="测试6",WorkcenterName="工作中心f"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0035",ProcessName="测试7",WorkcenterName="工作中心g"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0040",ProcessName="测试8",WorkcenterName="工作中心t"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0045",ProcessName="测试9",WorkcenterName="工作中心y"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0050",ProcessName="测试10",WorkcenterName="工作中心u"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0055",ProcessName="测试11",WorkcenterName="工作中心ww"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0060",ProcessName="测试11",WorkcenterName="工作中心g"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0065",ProcessName="测试12",WorkcenterName="工作中心t"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0070",ProcessName="测试13",WorkcenterName="工作中心y"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0075",ProcessName="测试14",WorkcenterName="工作中心u"},
                            new PrintHeadItem{ ProcessCardNo="202411010002",ProcessNumber="0080",ProcessName="测试15",WorkcenterName="工作中心ww"},
                        }
                    },
                    new PrintHead
                    {
                        ProjectCode = "PROJ202410310009",
                        ProductCode = "2024-011-001-0004",
                        PartDrawingNo = "0403",
                        PartName = "零件3",
                        ProdBatch = "001",
                        ProdQuantity = "10",
                        StartDate = "2024-09-27",
                        MaterialCode = "P-987654321",
                        MaterialName = "小米15_晓龙8Gen",
                        MaterialBatchNo = "36545-98652",
                        MaterialSpec = "59881工 655",
                        PartsManufacturable = "10",
                        MaterialItemNo = "ASF78945",
                        ProcessCardNo = "202410110003"
                    }
                }
            };
            return Ok(print);
        }

        /// <summary>
        /// 获取部件示例数据
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        [HttpPost(Name = "GetDemoDatatwo")]
        public IActionResult GetDemoDatatwo()
        {
            // 示例数据源
            var print = new Print()
            {
                PrintName = "张三",
                FormatCode = "JL-8.5.1-01",
                ProcessCardName = "部/总装工艺流程卡（正面）",
                PrintHeads = new List<PrintHead>()
                {
                    new PrintHead{
                        ProductCode = "0404-005-920",
                        PartDrawingNo = "0403",
                        PartName = "零件1",
                        ProdBatch = "001",
                        ProdQuantity = "10",
                        MaterialItemNo = "PROD46578911",
                        ProcessCardNo = "202411020001",//流程卡号：
                        Items = new List<PrintHeadItem>()
                        {
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0005",ProcessName="测试1",WorkcenterName="工作中心1"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0010",ProcessName="测试2",WorkcenterName="工作中心2"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0015",ProcessName="测试3",WorkcenterName="工作中心3"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0020",ProcessName="测试4",WorkcenterName="工作中心4"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0025",ProcessName="测试5",WorkcenterName="工作中心5"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0030",ProcessName="测试6",WorkcenterName="工作中心6"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0035",ProcessName="测试7",WorkcenterName="工作中心7"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0040",ProcessName="测试8",WorkcenterName="工作中心8"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0045",ProcessName="测试9",WorkcenterName="工作中心9"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0050",ProcessName="测试10",WorkcenterName="工作中心10"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0055",ProcessName="测试11",WorkcenterName="工作中心11"},
                            new (){ ProcessCardNo="202409010001",ProcessNumber="0060",ProcessName="测试12",WorkcenterName="工作中心12"},
                        }
                    },
                    new PrintHead{
                        ProductCode = "0404-005-920",
                        PartDrawingNo = "0403",
                        PartName = "零件1",
                        ProdBatch = "001",
                        ProdQuantity = "10",
                        MaterialItemNo = "PROD123456",
                        ProcessCardNo = "202411020002",
                        Items = new List<PrintHeadItem>()
                        {
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0005",ProcessName="测试1",WorkcenterName="工作中心1"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0010",ProcessName="测试2",WorkcenterName="工作中心2"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0015",ProcessName="测试3",WorkcenterName="工作中心3"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0020",ProcessName="测试4",WorkcenterName="工作中心4"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0025",ProcessName="测试5",WorkcenterName="工作中心5"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0030",ProcessName="测试6",WorkcenterName="工作中心6"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0035",ProcessName="测试7",WorkcenterName="工作中心7"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0040",ProcessName="测试8",WorkcenterName="工作中心8"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0045",ProcessName="测试9",WorkcenterName="工作中心9"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0050",ProcessName="测试10",WorkcenterName="工作中心10"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0055",ProcessName="测试11",WorkcenterName="工作中心11"},
                            new (){ ProcessCardNo="202411020002",ProcessNumber="0060",ProcessName="测试12",WorkcenterName="工作中心12"},
                        }
                    }
                }
            };



            return Ok(print);
        }
        /// <summary>
        /// 零件导出PDF，多个PDF合并为一个
        /// </summary>
        /// <returns></returns>
        [HttpPost(Name = "GetPdf")]
        public IActionResult GetPdf(Print print)
        {
            //返回pdf物理地址
            string filePath = generate.GetorderPDF(print);
            return Ok(filePath);
        }
        /// <summary>
        /// 零件导出Excel，多个Excel合成一个
        /// </summary>
        /// <returns></returns>
        [HttpPost(Name = "GetExcel")]
        public IActionResult GetExcel(Print print)
        {

            //返回Excel物理地址
            string filePath = generate.GetorderExcel(print);

            return Ok(filePath);
        }

        /// <summary>
        /// 部整件导出PDF，多个PDF合并为一个
        /// </summary>
        /// <returns></returns>
        [HttpPost(Name = "GetPdftwo")]
        public IActionResult GetPdftwo(Print print)
        {
            //返回pdf物理地址
            string filePath = generate.GetorderPDFtwo(print);
            return Ok(filePath);
        }

        /// <summary>
        /// 部整件导出Pexcel，多个excel合并为一个
        /// </summary>
        /// <returns></returns>
        [HttpPost(Name = "GetExceltwo")]
        public IActionResult GetExceltwo(Print print)
        {
            string filePath = generate.GetorderExcelTwo(print);
            return Ok(filePath);
        }

        /// <summary>
        /// 使用文件流下载文件
        /// </summary>
        /// <param name="filepath">文件物理地址</param>
        /// <returns></returns>
        [HttpPost(Name = "Downloadfile")]
        public IActionResult Downloadfile(string filepath)
        {
            // 检查文件是否存在
            if (!System.IO.File.Exists(filepath))
            {
                return NotFound("File not found.");
            }
            try
            {
                var (fileStream, contentType, fileDownloadName) = generate.DownloadFile(filepath);
                return File(fileStream, contentType, fileDownloadName);
            }
            finally 
            {
                // 下载完成删除文件
                //System.IO.File.Delete(filepath);
            }
        }
        [HttpGet]
        public IActionResult Test()
        {
            var id = Guid.NewGuid();

            var id1 = id.ToString().Replace("-", "");
            var id2 = id.ToString("N");

            return Ok();
        }
    }
}
